import numpy as np

# 之前去不去看电影的条件(天气，同伴，价格)
condition = np.array([[1, 1, 1],
                      [1, 0, 1],
                      [0, 1, 0],
                      [1, 1, 0]])
# 之前去不去看电影的最终决定
result = np.array([[1, 1, 0, 0]]).T


def fp(param):
    z = np.dot(param, weights)
    return 1 / (1 + np.exp(-z))


def bp(y, output):
    error = y - output
    slope = output * (1 - output)
    return error * slope


if __name__ == '__main__':

    # 初始化一个3个元素的weights, 并归一化到[-1,1]
    np.random.seed(1)
    weights = 2 * np.random.random((3, 1)) - 1
    weights = np.array([[-0.27],[0.83],[-0.41]])
    print(weights)
    # 循环1000次，调整参数
    for it in range(1000):
        output = fp(condition)
        delta = bp(result, output)
        weights = weights + np.dot(condition.T, delta)
    print(weights)
    print(fp([1, 1, 0]))
